package com.winit.openapi.apiadapter.controller.validator;

import java.math.BigDecimal;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.winit.openapi.constants.ErrorCode;
import com.winit.openapi.exception.ApiException;
import com.winit.openapi.util.ValidateUtil;

public class MerchandiseValidator {

    /**
     * 验证必填字段及类型验证
     * 
     * @param product
     */
    public static void validateRegisterMerchandise(JSONObject json) {
        JSONArray jsonArr = json.getJSONArray("productList");
        if (jsonArr == null || jsonArr.size() == 0) {
            throw new ApiException(ErrorCode.FIELD_NOT_NULL, "productList");
        }
        for (int i = 0; i < jsonArr.size(); i++) {
            JSONObject jo = (JSONObject) jsonArr.get(i);
            ValidateUtil.valid(jo, "productCode", true, false, 100);// 产品编码
            ValidateUtil.valid(jo, "chineseName", true, false, 255);// 中文名
            ValidateUtil.valid(jo, "englishName", true, false, 255);// 英文名
            ValidateUtil.valid(jo, "specification", false, false, 120);
            // validatorEn(jo.getString("englishName"), "englishName");

            /*
             * ValidateUtil.valid(jo, "categoryOne", true, false, 22);// 产品类别1
             * if (isEixtValue(jo, "categoryTwo")) ValidateUtil.valid(jo,
             * "categoryTwo", true, false, 22);// 产品类别2 if (isEixtValue(jo,
             * "categoryThree")) ValidateUtil.valid(jo, "categoryThree", true,
             * false, 22);// 产品类别3
             */
            ValidateUtil.validNotNull(jo, "registeredWeight");
            ValidateUtil.validNumberBiggerThanZero(jo, "registeredWeight");// 注册重量
            ValidateUtil.valid(jo, "fixedVolumeWeight", true, false, 1);// 是否固定体积

            ValidateUtil.validByRegex(jo, "fixedVolumeWeight", "[YN]", "fixedVolumeWeight");

            ValidateUtil.valid(jo, "displayPageUrl", true, false, 1000);// 英文名

            // ValidateUtil.validDoubleMinValue(jo, true, "registeredLength",
            // 0);// 长
            ValidateUtil.validNumberBiggerThanZero(jo, "registeredLength");
            ValidateUtil.valid(jo, "registeredLength", true, false, 10);
            ValidateUtil.validNumberBiggerThanZero(jo, "registeredWidth");// 宽
            ValidateUtil.valid(jo, "registeredWidth", true, false, 10);
            ValidateUtil.validNumberBiggerThanZero(jo, "registeredHeight");// 高
            ValidateUtil.valid(jo, "registeredHeight", true, false, 10);
            ValidateUtil.valid(jo, "branded", true, false, 1);// 是否有品牌
            // YorNtoTrueOrFalse(jo.getString("branded"), "branded");
            ValidateUtil.validByRegex(jo, "branded", "[YN]", "branded");

            if ("Y".equals(jo.getString("branded"))) {// 如果有品牌
                ValidateUtil.valid(jo, "brandedName", true, false, 100);// 品牌名
                ValidateUtil.valid(jo, "model", true, false, 100);//
            }
            ValidateUtil.valid(jo, "exportCountry", true, false, 22);// 出口国
            ValidateUtil.valid(jo, "inporCountry", true, false, 22);// 进口国
//            importCountryExportCountry(jo, "exportCountry");
//            importCountryExportCountry(jo, "inporCountry");
            // ValidateUtil.validDoubleMinValue(jo, true, "inportDeclaredvalue",
            // 0);// 进口申报价
            ValidateUtil.validNotNull(jo, "inportDeclaredvalue");
            ValidateUtil.validNotNull(jo, "exportDeclaredvalue");
            ValidateUtil.validNumberBiggerThanZero(jo, "inportDeclaredvalue");
            ValidateUtil.validNumberBiggerThanZero(jo, "exportDeclaredvalue");
            // ValidateUtil.validDoubleMinValue(jo, true, "exportDeclaredvalue",
            // 0);// 出口申报价
            ValidateUtil.valid(jo, "inportDeclaredvalue", true, false, 14);// 进口申报价
            ValidateUtil.valid(jo, "exportDeclaredvalue", true, false, 14);// 出口申报价

            ValidateUtil.valid(jo, "battery", true, false, 1);// 是否有品牌
            // YorNtoTrueOrFalse(jo.getString("battery"), "battery");
            ValidateUtil.validByRegex(jo, "battery", "[YN]", "battery");

            String stockOutRate = jo.containsKey("stockOutRate") ? jo.getString("stockOutRate") : null;
            String leadTime = jo.containsKey("leadTime") ? jo.getString("leadTime") : null;
            String rmentLeadTime = jo.containsKey("rmentLeadTime") ? jo.getString("rmentLeadTime") : null;

            if (stockOutRate != null) {
                BigDecimal stockOutRateB = new BigDecimal(stockOutRate);
                if (stockOutRateB.intValue() > 100 || stockOutRateB.intValue() <= 0) {
                    throw new ApiException(ErrorCode.MMS_STOCKOUTRATE_NOT_EXSIT);
                }
            }
            if (leadTime != null) {
                BigDecimal leadTimeB = new BigDecimal(leadTime);
                if (leadTimeB.intValue() <= 0 || leadTimeB.intValue() > 9999) {
                    throw new ApiException(ErrorCode.MMS_LEADTIME_NOT_EXSIT);
                }
            }
            if (rmentLeadTime != null) {
                BigDecimal rmentLeadTimeB = new BigDecimal(rmentLeadTime);
                if (rmentLeadTimeB.intValue() <= 0 || rmentLeadTimeB.intValue() > 9999) {
                    throw new ApiException(ErrorCode.MMS_REPLENISHMENT_NOT_EXSIT);
                }
            }
        }

    }

    public static void validateUpdateMerchandise(JSONObject json) {
        ValidateUtil.valid(json, "state", true, false, 25);
        ValidateUtil.valid(json, "productCode", true, false, 40);
        ValidateUtil.valid(json, "specification", false, false, 120);

        // ValidateUtil.validByUpdate(json, "englishName", true, false, 255);
        // ValidateUtil.validByUpdate(json, "categoryOne", true, false, 22);
        // ValidateUtil.validByUpdate(json, "categoryTwo", true, false, 22);
        // ValidateUtil.validByUpdate(json, "categoryThree", true, false, 22);
        if (json.containsKey("chineseName")) {
            ValidateUtil.valid(json, "chineseName", true, false, 255);
        }
        if (json.containsKey("englishName")) {
            ValidateUtil.valid(json, "englishName", true, false, 255);
        }
        if (json.containsKey("categoryOne")) {
            ValidateUtil.valid(json, "categoryOne", true, false, 22);
        }
        if (json.containsKey("categoryTwo")) {
            ValidateUtil.valid(json, "categoryTwo", true, false, 22);
        }
        if (json.containsKey("categoryThree")) {
            ValidateUtil.valid(json, "categoryThree", true, false, 22);
        }
        if (json.containsKey("displayPageUrl")) {
            ValidateUtil.valid(json, "displayPageUrl", true, false, 1500);
        }
        if (json.containsKey("chineseName")) {
            ValidateUtil.valid(json, "chineseName", true, false, 255);
        }
        if (json.containsKey("chineseName")) {
            ValidateUtil.valid(json, "chineseName", true, false, 255);
        }

        if (json.containsKey("registeredWeight")) {
            ValidateUtil.validNumberBiggerThanZero(json, "registeredWeight");
        }
        if (json.containsKey("registeredLength")) {
            ValidateUtil.validNumberBiggerThanZero(json, "registeredLength");
        }
        if (json.containsKey("registeredWidth")) {
            ValidateUtil.validNumberBiggerThanZero(json, "registeredWidth");
        }
        if (json.containsKey("registeredHeight")) {
            ValidateUtil.validNumberBiggerThanZero(json, "registeredHeight");
        }

        // ValidateUtil.validDoubleMinValueByUpdate(json, true,
        // "registeredWeight", 0);
        // ValidateUtil.validDoubleMinValueByUpdate(json, true,
        // "registeredLength", 0);
        // ValidateUtil.validDoubleMinValueByUpdate(json, true,
        // "registeredWidth", 0);
        // ValidateUtil.validDoubleMinValueByUpdate(json, true,
        // "registeredHeight", 0);

        // ValidateUtil.validValueIsLegal(json,"fixedVolumeWeight",ValidateUtil.yesOrNo);

        ValidateUtil.valid(json, "brandedName", false, false, 255);

        // ValidateUtil.validByUpdate(json, "brandedName", false, false, 255);
        // ValidateUtil.validByUpdate(json, "displayPageUrl", true, false,
        // 1500);// 必填
        // ValidateUtil.validByUpdate(json, "model", false, false, 255);
        // ValidateUtil.validByUpdate(json, "remark", false, false, 255);
        // ValidateUtil.validByUpdate(json, "FDACertID", false, false, 255);
        // ValidateUtil.validByUpdate(json, "FCCCertID", false, false, 255);
        // ValidateUtil.validByUpdate(json, "DOTCertID", false, false, 255);
        // ValidateUtil.validByUpdate(json, "otherCertID", false, false, 255);

        ValidateUtil.valid(json, "brandedName", false, false, 255);
        ValidateUtil.valid(json, "model", false, false, 255);
        ValidateUtil.valid(json, "remark", false, false, 255);
        ValidateUtil.valid(json, "FDACertID", false, false, 255);
        ValidateUtil.valid(json, "FCCCertID", false, false, 255);
        ValidateUtil.valid(json, "DOTCertID", false, false, 255);
        ValidateUtil.valid(json, "otherCertID", false, false, 255);

        // ValidateUtil.validValueIsLegal(json, "FCCControl",
        // ValidateUtil.yesOrNo);
        // ValidateUtil.validValueIsLegal(json, "FDAControl",
        // ValidateUtil.yesOrNo);
        // ValidateUtil.validValueIsLegal(json, "brand", ValidateUtil.yesOrNo);
        // ValidateUtil.validValueIsLegal(json, "battery",
        // ValidateUtil.yesOrNo);
        ValidateUtil.validByRegex(json, "brand", "[YN]", "brand");
        ValidateUtil.validByRegex(json, "battery", "[YN]", "battery");
        ValidateUtil.validByRegex(json, "FCCControl", "[YN]", "FCCControl");
        ValidateUtil.validByRegex(json, "FDAControl", "[YN]", "FDAControl");

    }

}
